package com.mybatis_plus.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatis_plus.demo.entity.Area;
import com.mybatis_plus.demo.entity.User;
import com.mybatis_plus.demo.mapper.AreaMapper;
import com.mybatis_plus.demo.mapper.UserMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class AreaController {

    @Resource
    private AreaMapper areaMapper;
    @Resource
    private UserMapper userMapper;


    @RequestMapping(value = "area/{id}", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> getAreaId(@PathVariable Long id) throws JsonProcessingException {
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("status", "error");
//
//        try {
//            Area area = areaService.findById(id);
//            jsonObject.put("status", "yes");
////            jsonObject.put("result", area);
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }

        Area area = areaMapper.selectById(id);
        jsonObject.put("area", area);
        Page<Area> page = new Page<>();
        page.setSize(1000);
        page.setCurrent(1);
        Page<Area> areaPage = areaMapper.selectPage(page, null);
        jsonObject.put("areaPage", areaPage);

//        User user = new User();
//        user.setAge(28);
//        user.setName("朱同学");
//        user.setCreateAcc("ces");
//        userMapper.insert(user);

        User user = userMapper.selectById(108);
        user.setName("test");
        user.setUpdateAcc("aaaa");
        userMapper.updateById(user);
        List<User> list = userMapper.findList();
        jsonObject.put("areaPage", list);

        return ResponseEntity.ok(jsonObject);
    }

    @RequestMapping(value = "areaPid/{pid}")
    public ResponseEntity<JSONObject> getAreaPid(@PathVariable Long pid) {


        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("status", "error");
//
//        try {
//            List<Area> areas = areaService.findByPid(pid);
//            jsonObject.put("status", "yes");
//            jsonObject.put("result", areas);
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }

        return ResponseEntity.ok(jsonObject);
    }
}
